package org.kotboot.kit;

import org.apache.commons.codec.binary.Base64;

import java.security.MessageDigest;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * AES加密解密
 */
public class AES {
 
    private static final String default_charset = "UTF-8";

    public static String encrypt(String content,String key){
        return encrypt(content,key,true);
    }
    
    /**
     * 加密
     * @param content 需要加密的内容
     * @param key 加密密码
     * @param md5Key 是否对key进行md5加密
     */
    public static String encrypt(String content,String key,boolean md5Key){
    	try {
			byte[] content_bytes = content.getBytes(default_charset);
			byte[] key_bytes = key.getBytes(default_charset);
			if (md5Key) {
                MessageDigest md = MessageDigest.getInstance("MD5");
                key_bytes = md.digest(key_bytes);
            }
            SecretKeySpec skeySpec = new SecretKeySpec(key_bytes, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/ISO10126Padding"); //"算法/模式/补码方式"
            IvParameterSpec ivps = new IvParameterSpec(key_bytes);//使用CBC模式，需要一个向量iv，可增加加密算法的强度
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivps);
            byte[] doFinal = cipher.doFinal(content_bytes);
            return Base64.encodeBase64String(doFinal);
		} catch (Exception e) {
			e.printStackTrace();
		} 
    	return null;
    }

    public static String decrypt(String content,String key){
        return decrypt(content,key,true);
    }
    
    /**
     * 解密
     * @param content 需要解密的内容
     * @param key 加密密码
     * @param md5Key 是否对key进行md5加密
     */
    public static String decrypt(String content,String key,boolean md5Key){
    	try {
    		byte[] content_bytes = Base64.decodeBase64(content);
			byte[] key_bytes = key.getBytes(default_charset);
            if (md5Key) {
                MessageDigest md = MessageDigest.getInstance("MD5");
                key_bytes = md.digest(key_bytes);
            }
            SecretKeySpec skeySpec = new SecretKeySpec(key_bytes, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/ISO10126Padding"); //"算法/模式/补码方式"
            IvParameterSpec ivps = new IvParameterSpec(key_bytes);//使用CBC模式，需要一个向量iv，可增加加密算法的强度
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivps);
            byte[] doFinal = cipher.doFinal(content_bytes);
            return new String(doFinal,default_charset);
        } catch (Exception ex) {
        }
        return null;
    }
    
    public static void main(String[] args) throws Exception {
    	String sendStr = "123";
        String aesKey = "1234567812345678";
        //以下是对数据进行加密
        String encrypt = AES.encrypt(sendStr, aesKey, false);
        System.out.println(encrypt);
        //以上是对数据进行加密
        String decrypt = AES.decrypt("zaWEIKLJ4n2Z/wm6Vd1xxQ", aesKey, false);
        System.out.println(decrypt);
        String md5 = Base64.encodeBase64String("123".getBytes(default_charset));
        System.out.println(md5);
        
	}
}
